/**
 * All Rights Reserved , Copyright (C) 2020 , 青岛鼎信通讯股份有限公司
 * BasePermissionManageService
 * 基础权限管理
 * 修改纪录
 * 2020-12-01 版本：1.0 zhanglongfei 创建。
 * 
 * @version 版本：1.0
 * @author 作者：zhanglongfei
 * 创建日期：2020-12-01
 */
package com.topscomm.base.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.apache.curator.shaded.com.google.common.base.Objects;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.topscomm.base.BaseSystemConst;
import com.topscomm.base.pojo.BasePermissionManageEntity;
import com.topscomm.base.service.IBaseCboUserExtendService;
import com.topscomm.base.service.IBasePermissionManageService;
import com.topscomm.base.service.impl.auto.BasePermissionManageServiceAuto;
import com.topscomm.pub.util.ConvertUtil;
import com.topscomm.pub.util.StringBufferProxy;
import com.topscomm.pub.util.StringUtil;

@Service(value = "basePermissionManageService")
@Scope(value = "singleton")
public class BasePermissionManageService extends BasePermissionManageServiceAuto implements IBasePermissionManageService {
	private static final String USERCODES = "usercodes";
	private static final String DEPTCODES = "deptcodes";
	private static final String TEAMIDS = "teamids";
	private static final String ROLEIDS = "roleids";
	private static final String SHARECODES = "sharecodes";
	// 前端传递的selectUser的key值对应关系
	private static Map<String, String> typeList = new HashMap<>();
	static {
		typeList.put(USERCODES, BaseSystemConst.ShareTypeEnum.User);
		typeList.put(DEPTCODES, BaseSystemConst.ShareTypeEnum.Dept);
		typeList.put(TEAMIDS, BaseSystemConst.ShareTypeEnum.Team);
		typeList.put(ROLEIDS, BaseSystemConst.ShareTypeEnum.Role);
		typeList.put(SHARECODES, BaseSystemConst.ShareTypeEnum.Share);
	}
	@Autowired
	IBaseCboUserExtendService baseCboUserExtendService;

	/***
	 * @description: 得到批量人员
	 * @param users
	 * @param sourceFlag
	 * @param sourceUserCodes
	 * @param sourceUserNames
	 * @return java.util.List<com.topscomm.base.pojo.BasePermissionManageEntity>
	 * @author: wangxinhua
	 * @date: 2021/3/24 13:57
	 * @modify:
	 */
	@Override
	public List<BasePermissionManageEntity> processSelectUsers(Map<String, Object> users, String sourceFlag, String sourceUserCodes, String sourceUserNames) {
		List<BasePermissionManageEntity> list = new ArrayList<>();
		if (users == null) {
			return list;
		}
		for (String key : typeList.keySet()) {
			if (users.containsKey(key)) {
				String userCode = ConvertUtil.convertToString(users.get(key));
				if (userCode.length() > 0) {
					BasePermissionManageEntity entity = new BasePermissionManageEntity();
					entity.setShareobjects(userCode);
					entity.setShareobjectsdesc(ConvertUtil
							.convertToString(users.get(key.replace("codes", "").replace("ids", "") + "names")));
					entity.setSharetype(typeList.get(key));
					entity.setSourceflag(sourceFlag);
					entity.setSourceusercodes(sourceUserCodes);
					entity.setSourceusernames(sourceUserNames);
					list.add(entity);
				}
			}
		}
		return list;
	}

	/**
	 * @description:批量保存
	 * @param sourceId
	 * @param sourceType
	 * @param list
	 * @author: zhanglongfei
	 * @date: 2020年11月2日下午5:30:12
	 * @modify:
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void batchSaveList(String sourceId, String sourceType, List<BasePermissionManageEntity> list) {
		StringBufferProxy sql = new StringBufferProxy();
		sql.appendLineSingle("sourcetype='{0}' and sourceid='{1}'", sourceType, sourceId);
		deleteByWhere(sql.toString());
		for (BasePermissionManageEntity item : list) {
			item.setSourceid(sourceId);
			item.setSourcetype(sourceType);
			insert(item);
		}
	}

	/**
	 * @description: 得到全部的参会人员
	 * @param selectUsers
	 * @return
	 * @author: wanghongwei
	 * @date: 2020-11-3 20:10:42
	 * @modify:
	 */
	@Override
	public List<Map<String, Object>> getAllAttendUser(Map<String, Object> selectUsers) {
		List<Map<String, Object>> resultList = new ArrayList<Map<String, Object>>();
		if (selectUsers == null) {
			return resultList;
		}
		for (String key : typeList.keySet()) {
			if (!selectUsers.containsKey(key)) {
				continue;
			}
			String shareObj = ConvertUtil.convertToString(selectUsers.get(key));
			if (!StringUtil.isEmpty(shareObj)) {
				if (Objects.equal(USERCODES, key)) {
					resultList.addAll(this.baseCboUserExtendService.searchUsersByUserCodes(shareObj));
				} else if (Objects.equal(DEPTCODES, key)) {
					resultList.addAll(this.baseCboUserExtendService.searchUsersByDeptCodes(shareObj));
				} else if (Objects.equal(TEAMIDS, key)) {
					resultList.addAll(this.baseCboUserExtendService.searchUsersByTeamIds(shareObj));
				} else if (Objects.equal(ROLEIDS, key)) {
					resultList.addAll(this.baseCboUserExtendService.searchUserByRoleIds(shareObj));
				} else if (Objects.equal(SHARECODES, key)) {
					resultList.addAll(this.baseCboUserExtendService.searchUsersByUserCodes(shareObj));
				}
			}
		}
		return resultList.stream().distinct().collect(Collectors.toList());
	}
}
